﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Input;

namespace QueenInject
{
    delegate void GUIcallback();

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            buttonToggleState.Text = "Start";
            buttonToggleState.Enabled = false;

            timerHandler = new EventHandler(timerFunction);

            globalKeyboardHook.start();
            globalKeyboardHook.setCtrlShiftSCallback(new GUIcallback(ctrlShiftSCallback));
            globalKeyboardHook.setCtrlShiftSpaceCallback(new GUIcallback(ctrlShiftSpaceCallback));
            globalKeyboardHook.setVCallback(new GUIcallback(vCallback));
        }
        ~Form1()
        {
            globalKeyboardHook.stop();
        }

        //second window
        Form2 form2 = new Form2();

        //mp3 file selection
        private void buttonMP3file_Click(object sender, EventArgs e)
        {
            if (openFileDialogMP3file.ShowDialog() == DialogResult.OK)
            {
                textBoxMP3file.Text = openFileDialogMP3file.FileName;
                buttonToggleState.Text = "Start";
                buttonToggleState.Enabled = true;
            }
        }
        private void textBoxMP3file_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        private void textBoxMP3file_DragDrop(object sender, DragEventArgs e)
        {
            string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            textBoxMP3file.Text = filePath[0];
            buttonToggleState.Text = "Start";
            buttonToggleState.Enabled = true;
        }

        //mp3 file playing
        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
        private void playMP3file()
        {
            mciSendString("open \"" + textBoxMP3file.Text + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
            mciSendString("play MediaFile", null, 0, IntPtr.Zero);
        }
        private void stopMP3file()
        {
            mciSendString("close MediaFile", null, 0, IntPtr.Zero);
        }

        //timer
        private int secondsPassed = 0;
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private void timerFunction(Object sender, EventArgs e)
        {
            secondsPassed++;

            int countdown = ((int)numericUpDownInterval.Value) - secondsPassed;
            labelCountdown.Text = countdown.ToString();
            form2.updateCounter(countdown.ToString());

            if (secondsPassed == ((int)numericUpDownInterval.Value))
            {
                secondsPassed = 0;
                stopMP3file();
                playMP3file();
            }
        }
        private EventHandler timerHandler;

        //start and stop timer
        private void startTimer()
        {
            //start timer
            secondsPassed = 0;
            timer.Tick += timerHandler;
            timer.Interval = 1000;
            timer.Start();

            //modify GUI
            textBoxMP3file.Enabled = false;
            buttonMP3file.Enabled = false;
            numericUpDownInterval.Enabled = false;
            buttonToggleState.Text = "Stop";
        }
        private void stopTimer()
        {
            //stop timer
            timer.Tick -= timerHandler;
            timer.Stop();

            //modify GUI
            textBoxMP3file.Enabled = true;
            buttonMP3file.Enabled = true;
            numericUpDownInterval.Enabled = true;
            buttonToggleState.Text = "Start";
        }

        //user input
        private void ctrlShiftSCallback()
        {
            if (buttonToggleState.Text == "Start")
            {
                startTimer();
            }
            else if (buttonToggleState.Text == "Stop")
            {
                stopTimer();
            }
        }
        private void ctrlShiftSpaceCallback()
        {
            //reset timer if timer is active
            if (buttonToggleState.Text == "Stop")
            {
                stopTimer();
                startTimer();
            }
        }
        private void vCallback()
        {
            //reset timer if timer is active
            /*if (buttonToggleState.Text == "Stop")
            {
                stopTimer();
                startTimer();
            }*/
        }
        private void buttonToggleState_Click(object sender, EventArgs e)
        {
            if (buttonToggleState.Text == "Start")
            {
                startTimer();

                if (form2.Visible == false)
                {
                    form2.Visible = true;
                }
            }
            else if (buttonToggleState.Text == "Stop")
            {
                stopTimer();
            }
        }
    }
}
